package com.mzpai.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.aliyun.opensearch.sdk.dependencies.com.google.gson.JsonObject;
import com.gexin.fastjson.JSONObject;
import com.mzpai.product.enums.UserActionEnum;
import com.mzpai.product.model.BoardReport;
import com.mzpai.product.model.SystemStore;
import com.mzpai.product.model.UserAction;
import com.mzpai.product.service.BoardReportService;
import com.mzpai.product.service.SystemStoreService;
import com.mzpai.product.service.UserActionService;
import com.mzpai.product.vo.BoardReportVo;
import com.mzpai.util.StringUtils;
import com.mzpai.vo.ResponseVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "流量统计接口")
@RestController
@RequestMapping("/board")
public class BoardController {
	
	 protected Logger logger = LoggerFactory.getLogger(BoardReportService.class);
	 
	@Autowired
	private BoardReportService boardReportService;
	@Autowired
	private SystemStoreService systemStoreService;
	@Autowired
	private UserActionService userActionService;
	@ApiOperation(value = "根据门店ID，查询首页看板接口")
	@RequestMapping(value = "/queryBoardReport")
	public ResponseVo<List<BoardReportVo>> queryFlowReport(@RequestParam(value = "storeId", required = true) String storeId) throws Exception {
	
		logger.info("msg1=开始根据门店ID，查询首页看板接口,,method=queryFlowReport,,storeId={}",storeId);
		ResponseVo<List<BoardReportVo>> res = new ResponseVo<List<BoardReportVo>>();
		List<BoardReportVo> list = new ArrayList<BoardReportVo>();
		//1.今日访客量；
		BoardReportVo o = new BoardReportVo();
		o.setStoreId(storeId);
		o.setType("1");
		o.setCount(addFlowReport(storeId));
		o.setTypeName("今日访客量");
		list.add(o);
		//2.今日营业额
		o = new BoardReportVo();
		o.setStoreId(storeId);
		o.setType("2");
		o.setCount(addOrderAmountReport(storeId));
		o.setTypeName("今日营业额");
		list.add(o);
		//3.今日订单数
		o = new BoardReportVo();
		o.setStoreId(storeId);
		o.setType("3");
		o.setCount(addOrderCountReport(storeId));
		o.setTypeName("今日订单数");
		list.add(o);
		res.setResult(list);
		logger.info("msg1=结束根据门店ID，查询首页看板接口,,method=queryFlowReport,,storeId={},,list={}",storeId,JSON.toJSONString(list));
		
		/**统计后需要增加日志*/
		addUserActionLog(Integer.parseInt(storeId));
		
		return res;
	}
	
	
	private void addUserActionLog(Integer storeId) {
		Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
            	//logger.info("msg1=请求参数信息={}", JSON.toJSON(operateLog));
            	try{
            		UserAction target = new UserAction();
            		target.setStoreId(storeId);
            		target.setCreateTime(new Date());
            		target.setEvent(UserActionEnum.stroe_login.getCode());
            		SystemStore sytemStore = systemStoreService.queryById(String.valueOf(storeId));
            		target.setStoreName(sytemStore.getName());
            		target.setCreateUser(sytemStore.getPhone());
            		target.setParam(JSONObject.toJSONString(sytemStore));
					userActionService.save(target);
            	}catch(Exception e) {logger.info("", e);}
            }
        });
        thread.start();
	}

	/**
	 * 添加流量
	 * @param storeId
	 * @return
	 */
	private Double addFlowReport(String storeId) {
		if(StringUtils.isBlank(storeId) || "0".equals(storeId)) {
			return 0D;
		} else {
			return boardReportService.queryFlowReport(storeId).getCount();
		}
	}
	
	/**
	 * 根据门店ID，今日营业额接口
	 * @param storeId
	 * @return
	 */
	private Double addOrderAmountReport(String storeId) {
		if(StringUtils.isBlank(storeId) || "0".equals(storeId)) {
			return 0D;
		} else {
			BoardReport o = boardReportService.queryOrderAmounReport(storeId);
			if(o != null) {
				Double count=o.getCount();
				logger.info("msg=统计今日营业额,,method=addOrderAmountReport,,count={}",count);
				return count;
			}
			return 0D;
		}
	}
	
	
	/**
	 * 根据门店ID，今日已支付订单数量订单数接口
	 * @param storeId
	 * @return
	 */
	private Double addOrderCountReport(String storeId) {
		if(StringUtils.isBlank(storeId) || "0".equals(storeId)) {
			return 0D;
		} else {
			BoardReport o = boardReportService.queryOrderReport(storeId);
			if(o != null) {
				return o.getCount();
			}
			return 0D;
		}
	}
}
